package lion.blog.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.lang.StringUtils;

import lion.blog.model.BlogMenu;
import lion.blog.service.BlogService;
import lion.framework.core.bean.BeanFactory;
import lombok.Setter;

/**
 * 菜单列表
 * 
 * ul.menus>li>a.current
 * 
 * @author lion
 * @email hl_0074@sina.com
 * @date 2015年9月21日
 */
public class MenuTag extends SimpleTagSupport {

	@Setter
	private String current;

	@Override
	public void doTag() throws JspException, IOException {

		BlogService service = BeanFactory.getBeanFactory(BeanFactory.PLATFORM_BEAN_FACTORY).get("blog.BlogService");

		List<BlogMenu> menus = service.getBlogMenus();

		StringBuilder builder = new StringBuilder();
		builder.append("<ul class=\"menus\">");
		for (BlogMenu menu : menus) {
			if (StringUtils.equals(current, menu.getCode())) {
				builder.append("<li class=\"current\"><a href=\"" + menu.getUrl() + "\">" + menu.getName() + "</a></li>");
			} else {
				builder.append("<li><a href=\"" + menu.getUrl() + "\">" + menu.getName() + "</a></li>");
			}
		}

		builder.append("</ul>");

		getJspContext().getOut().write(builder.toString());
	}
}
